<template>
	<view class="" >
		<view class="address">
			<view v-if="addressList.length>0" @click="address_pop_show = true" class="address_yes">
				<view class="address_yes_name">
					收货人信息：{{address.name}}（{{address.phone}}）
				</view>
				<view class="address_yes_address">
					收货地址：{{`${address.province} ${address.city} ${address.area} ${address.address}` }} 
				</view>
				
			</view>
			<view v-else @click="toUrl('/pages/addAddresss/addAddresss')" class="address_no">
				<u-icon name="plus-people-fill" color="#666" size="40"></u-icon>
				<span style="margin-left: 25rpx;color: #666; font-size: 26rpx;">添加收货人</span>
			</view>
		</view>
		<view class="xian"></view>
		<u-gap height="20" bg-color="#efeff4"></u-gap>
		<!-- 商品信息 -->
		<view v-for="(item,index) in order_info.goods" :key="index" class="shop_info">
			<u-image :src="item.pic" bg-color="#ccc"  width="150rpx" height="150rpx"></u-image>
			<view class="shop_info_right">
				<view class="shop_info_right_title uni-ellipsis-3">
					{{item.name}}
				</view>
				<view class="shop_info_right_sku_info" style="justify-content: flex-start; ">
					<span class="sku_num" style="font-weight: 400; font-size: 24rpx;"> {{item.spec.sku}}</span>
				</view>
				<view class="shop_info_right_sku_info">
					<span class="sku_price">JPY Ұ {{item.spec.price}}</span>
					<span class="sku_num"> x {{item.spec.num?item.spec.num:url_info.sku_num}}</span>
				</view>
			</view>
		</view>
		<u-gap height="20" bg-color="#efeff4"></u-gap>
		<!-- 订单信息 -->
		<view class="order_info_box">
			<view class="order_info_item">
				<span>商品金额</span>
				<span class="order_info_item_price">JPY Ұ {{order_info.goods_price}}</span>
			</view>
			<view class="order_info_item">
				<span>运费</span>
				<span class="order_info_item_price">{{Number(order_info.postage_price)>0?`JPY Ұ ${order_info.postage_price}`:'免邮'}}</span>
			</view>
			<view class="order_info_item">
				<span></span>
				<div style="display: flex; flex-direction: column; justify-content: flex-end; align-items: flex-end;">
					<span class="order_info_item_ying">应付: <span style="margin-left: 32rpx;" class="order_info_item_price">JPY Ұ {{order_info.pay_price}}</span> </span>
					<div style="color: #aaa; font-size: 22rpx;">
						≈{{order_info.cny_price}}RMB
					</div>
				</div>
			</view>
		</view>
		<!-- 买家留言 -->
		<u-gap height="20" bg-color="#efeff4"></u-gap>
		<view class="">
			<textarea v-model="remark"  class="make" placeholder-class="plac" maxlength="100" :show-confirm-bar="true" placeholder="请输入卖家留言" id="" c></textarea>
		</view>
		<u-gap height="20" bg-color="#fff"></u-gap>
		<view class="btn_box_submit">
			<u-button @click="OrderSettlement()" style="background-color: #6993b7; color:#fff;font-size: 26rpx;" :hair-line="false" :ripple="true">确认购买</u-button>
		</view>
		<!-- 地址选择 -->
		<u-popup v-model="address_pop_show" mode="bottom" border-radius="14">
			<view class="address_pop_box">
				<view class="address_pop_top">
					<view class="fahup_pop_box_title">
						选择邮寄地址
						<view class="search_box">
							<input @confirm="getAddress" placeholder="请输入收货人姓名" type="text" v-model="address_text"  />
							<u-icon @click="getAddress" name="search" color="#666" size="28"></u-icon>
						</view>
					</view>
					<view @click="toUrl('/pages/addAddresss/addAddresss')" class="addAddress">
						<image style="width: 24rpx; height: 24rpx;margin-right: 8rpx;"
							src="https://www.mengkahuyu.com/index_png/add_icon.png" mode=""></image>
						<span>添加收件人</span>
					</view>
				</view>
				<scroll-view class="scroll_y_address" scroll-y="true">
					<view class="scroll_padding">
						<view v-for="(ite,ind) in addressList" :key="ind"
							class="address_item">
							<view  @click="chooseAddress(ite ,ind)" class="address_left">
		
								<image v-if="ite.isChoose" style="width: 24rpx; height: 24rpx;"
									src="https://www.mengkahuyu.com/index_png/radio_choose.png" mode=""></image>
								<image v-else style="width: 24rpx; height: 24rpx;"
									src="https://www.mengkahuyu.com/index_png/radio.png" mode=""></image>
							</view>
							<view  @click="chooseAddress(ite ,ind)" class="address_center">
								<view class="centern_top">
									<span class="user_name">
										{{ite.name}}
									</span>
									<span class="user_phone">
										{{ite.phone}}
									</span>
								</view>
								<view class=" centern_top centern_center">
									{{ite.province}}
									{{ite.city}}
									{{ite.area}}
								</view>
								<view class="centern_center centern_top">
									{{ite.address}}
								</view>
							</view>
							<view @click="toUrl('/pages/addAddresss/addAddresss?id='+ite.id)" class="address_left">
								<image  style="width: 24rpx; height: 24rpx;"
									src="https://www.mengkahuyu.com/index_png/edit.png" mode=""></image>
							</view>
		
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
		
	</view>
</template>

<script>
    export default {
		
        data() {
            return {
				di_point:'',//抵扣积分
				address_text:'',
				address_pop_show:false,
				addressList:[],
				address:{},
				url_info:{},
				order_info:{}, //订单详情
				coupon_list:[],
				choose_coupon:{},//已选择的优惠劵
				coupon_show:false,
				remark:'',
				userInfo:{},
				disable:false
            }
        },
        methods: {
           
        },
		onShow() {
			this.disable = false
			this.getInfo()
		},
		onLoad(params){
			this.url_info = JSON.parse(params.Info)
			this.seckill_id = params.seckill_id
			this.seckill_goods_id = params.seckill_goods_id
			// console.log(this.url_info)
		},
		mounted() {
			
		},
		methods: {
			getInfo(){
				uni.showLoading({
					title:'加载中...'
				})
				var data = {}
				this.$wqzApi.user_index(data).then(res=>{
					this.userInfo = res.data
					uni.setStorageSync('user_info' , res.data)
					this.getAddress()
					
				})
			},
			ponit_change(e , max){
				if(e.detail.value > max){
					this.di_point = max
					this.$nextTick(() => {
						this.di_point = max
						e.detail.value = max
					});
				}
				this.Settlement_information()
			},
			// 获取可用优惠劵列表
			get_coupon_list(){
				var data = {
					"goods_id": this.url_info.gid?this.url_info.gid:null,
					"cart_id":this.url_info.cart_ids?this.url_info.cart_ids:null,
					"order_price":this.order_info.goods_price
				}
				this.$gugoApi.GoodsOrder_order_coupon(data).then(res=>{
					this.coupon_list = res.data
					this.coupon_list.forEach((item,index)=>{
						if(item.is_default ==1) {
							this.choose_coupon = item
							this.Settlement_information()
						}
					})
				})
			},
			// 选择优惠劵
			choose_coupon_list(ite , ind){
				if(this.choose_coupon.id == ite.id){
					return
				}
				this.coupon_list.forEach((item , index)=>{
					if(ind == index){
						this.choose_coupon = item
						this.coupon_list[index].is_default = 1
						this.coupon_show = false
						this.Settlement_information()
					}else{
						this.coupon_list[index].is_default = 0
					}
				})
			},
			getAddress(){
				// 获取地址列表
				this.$wqzApi.UserAddress_list({name:this.address_text}).then(res => {
					res.data.list.forEach((item, index) => {
						res.data.list[index].isChoose = false
					})
					this.addressList = res.data.list
					if(this.addressList.length>0){
						this.address = res.data.list[0]
						this.Settlement_information()
					}else{
						
						uni.showToast({
							title:'请添加收货人',
							icon:'none',
						})
					}
				})
			},
			// 结算信息
			Settlement_information(){
				// var date = {
				// 	"cart_id": this.url_info.cart_ids?this.url_info.cart_ids:'',
				// 	"address_id":this.address.id,
				// 	"goods_sku_id": this.url_info.sku_id?this.url_info.sku_id:'',
				// 	"goods_id": this.url_info.gid?this.url_info.gid:'',
				// 	"goods_num": this.url_info.sku_num?this.url_info.sku_num:'',
				// 	"coupon_record_id":this.choose_coupon.id?this.choose_coupon.id:'',
				// 	'user_points':this.di_point,
				// 	seckill_id:this.seckill_id,
				// }
				var date = {
					"address_id": this.address.id, //地址id
					"goods_id": this.url_info.gid?this.url_info.gid:'', //商品id(购物车有值的时候关闭)
					"goods_sku_id": this.url_info.sku_id?this.url_info.sku_id:'', //商品规格id(购物车有值的时候关闭)
					"goods_num": this.url_info.sku_num?this.url_info.sku_num:'', //商品数量(购物车有值的时候关闭)
					"seckill_goods_id": this.seckill_goods_id,
					'seckill_id':this.seckill_id,
				}
				
				this.$wqzApi.GoodsOrderSeckill_OrderSettlement(date).then(res=>{
					res.data.goods.map((item,index)=>{
						var pic_arr = item.pic.split(',')
						item.pic = pic_arr[0]
					})
					this.order_info = res.data
					
					if(res.data.goods_points_deduction_points< this.userInfo.points){
						this.di_point = res.data.goods_points_deduction_points
					}else{
						this.di_point = this.userInfo.points
					}
					if(!this.disable){
						// this.Settlement_information()
						this.disable = true
					}
					if(this.coupon_list.length<=0){
						// this.get_coupon_list()
					}
					uni.hideLoading()
				}).catch(err=>{
					uni.hideLoading()
					// uni.navigateTo({
					// 	url:`/pages/good_order_List/good_order_List?current=1`
					// })
				})
				
			},
			// 确认下单
			OrderSettlement(){
				// {
				// 					"cart_id": this.url_info.cart_ids?this.url_info.cart_ids:'',
				// 					"address_id":this.address.id,
				// 					"goods_sku_id": this.url_info.sku_id?this.url_info.sku_id:'',
				// 					"goods_id": this.url_info.gid?this.url_info.gid:'',
				// 					"num": this.url_info.sku_num?this.url_info.sku_num:'',
				// 					"coupon_record_id":this.choose_coupon.id?this.choose_coupon.id:'',
				// 					'order_remark':this.remark,
				// 					'goods_points_deduction_points':this.di_point,
				// 					'goods_points_gift_points':this.order_info.goods_points_gift_points
				// 				}
				var date = {
					"seckill_id":this.seckill_id,
					"address_id":this.address.id,
					"goods_sku_id":this.url_info.sku_id?this.url_info.sku_id:'',
					"goods_id":this.url_info.gid?this.url_info.gid:'',
					"num":this.url_info.sku_num?this.url_info.sku_num:'',
					"order_remark":this.remark,
				}
				if(this.url_info.uid){
					//成为他人用户时传参数
					date.dealer_id = this.url_info.uid
				}
				uni.showLoading()
				this.$wqzApi.GoodsOrderSeckill_add(date).then(res=>{
					uni.hideLoading()
					var order_id = res.data
					var data = {
						order_id:order_id,
					}
					// console.log(data)
					// 
					
					// #ifdef H5
					var ua = navigator.userAgent.toLowerCase();
					    var isWeixin = ua.indexOf('micromessenger') !== -1;
					    if (!isWeixin) {
					      this.$wqzApi.guGo_pay_good(data).then(res=>{
					      	var url = res.data
							console.log(res.data,'请求路径')
					      	window.location.href = url
					      	
					      }).catch(err=>{
					      	
					      })
					    }else{
							console.log('微信浏览器')
					      this.$wqzApi.goods_pay_jsapi(data).then(res=>{
					      	console.log(res , '返回信息支付')
					      	var url = res.data
					      	window.location.href = url
					      	
					      }).catch(err=>{
					      	
					      })
					    }
						
					// #endif
					// #ifdef MP-WEIXIN
						this.$wqzApi.guGo_pay_good_wx(data).then(res=>{
							var url = res.data
							var pay_data = res.data
							wx.requestPayment({
							  timeStamp: pay_data.timeStamp,
							  nonceStr: pay_data.nonceStr,
							  package: pay_data.package,
							  signType: 'MD5',
							  paySign: pay_data.paySign,
							  success (res) {
								  uni.hideLoading()
								  uni.showToast({
								  	title:'支付成功',
									icon:'none'
								  })
								  setTimeout(()=>{
									  uni.navigateTo({
									  	url:`/pages/good_order_List/good_order_List?current=2`
									  })
								  },1200)
							  },
							  fail (res) {
								  uni.hideLoading()
								  setTimeout(()=>{
								  	uni.navigateTo({
								  		url:`/pages/good_order_List/good_order_List?current=1`
								  	})
								  },1200)
								
							  }
							})
						}).catch(err=>{
							
						})
					// #endif
					
				})
			},
			chooseAddress(ite, ind) {
				// 选择收货地址
				this.addressList.forEach((item, index) => {
					if (ind == index) {
						this.addressList[index].isChoose = true
						this.address = item
						this.address_pop_show = false
					} else {
						this.addressList[index].isChoose = false
					}
				})
				this.Settlement_information()
			},
			toUrl(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
    }
</script>

<style lang="scss">
 @import url(./order_pay.scss);
</style>